<?

/* $Id: Guid.php,v 1.0 2004/07/08 05:50:17 binzy Exp $ */

class Helper_Guid {
	
	var $valueBeforeMD5;
	var $valueAfterMD5;
	
	function Helper_Guid() {
		$this->getGuid ();
	}
	
	function getGuid() {
		$address = self::getLocalHost ();
		$this->valueBeforeMD5 = $address . ':' . self::currentTimeMillis () . ':' . self::nextLong ();
		$this->valueAfterMD5 = md5 ( $this->valueBeforeMD5 );
	}
	
	function get($md5 = false) {
		if ($md5) {
			return $this->valueAfterMD5;
		} else {
			return $this->valueBeforeMD5;
		}
	}
	
	function newGuid() {
		$Guid = new Guid ( );
		return $Guid;
	}
	
	function toString() {
		$raw = strtolower ( $this->valueAfterMD5 );
		return substr ( $raw, 0, 8 ) . '-' . substr ( $raw, 8, 4 ) . '-' . substr ( $raw, 12, 4 ) . '-' . substr ( $raw, 16, 4 ) . '-' . substr ( $raw, 20 );
	}
	
	function nextLong() {
		$tmp = rand ( 0, 1 ) ? '-' : '';
		return $tmp . rand ( 1000, 9999 ) . rand ( 1000, 9999 ) . rand ( 1000, 9999 ) . rand ( 100, 999 ) . rand ( 100, 999 );
	}
	
	function getLocalHost() {
		$IP = $_SERVER ["SERVER_ADDR"];		
		return strtolower ( $IP );
	}
	
	function currentTimeMillis() {
		list ( $usec, $sec ) = explode ( " ", microtime () );
		return $sec . substr ( $usec, 2, 3 );
	}

}

?>